﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.tremormedia.acudeo.admanager {
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;
    import flash.geom.*;
    import com.tremormedia.acudeo.events.*;
    import flash.net.*;
    import com.tremormedia.acudeo.controls.*;
    import com.tremormedia.acudeo.ad.enums.*;
    import com.tremormedia.acudeo.*;
    import net.comcast.logging.consoles.*;
    import com.tremormedia.acudeo.adunit.*;
    import net.comcast.logging.*;

    public class AdManager extends Sprite implements IAdManager {

        private var _loadTimer:Timer;
        private var _container:Sprite;
        private var _adManager:Object;
        private var _videoRegion:Rectangle;// = null
        private var _destroyCalled:Boolean;// = false
        private var _params:Object;
        private var _loadParams:Object;// = null
        private var _playlist:Object;// = null
        private var _loader:Loader;
        private var _volume:Object;
        private var _content:Object;// = null
        private var _prevProgress:Number;// = -1
        private var _adManagerLoaded:Boolean;// = false

        private static const DEFAULT_TIMEOUT:Number = 4000;
        private static const DEFAULT_HOST:String = "http://objects.tremormedia.com";

        private static var _instance:IAdManager = null;
        public static var initLogging:Boolean = true;

        public function AdManager(){
            if (initLogging){
                Logger.addConsole(new TraceConsole());
                Logger.addConsole(new FirebugConsole());
            };
            Logger.info(this, "AdManager");
        }
        public function playAd(_arg1:Object):void{
            trace(("loadAd " + _arg1));
        }
        public function setVideoRegion(_arg1:Rectangle):void{
            if (!_adManagerLoaded){
                _videoRegion = _arg1;
            } else {
                _adManager.setVideoRegion(_arg1);
            };
        }
        public function contentPaused():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to contentPaused before program loaded, ignoring");
                dispatchEvent(new AcudeoEvent({type:AcudeoEvent.ADS_COMPLETE}));
            } else {
                _adManager.contentPaused();
            };
        }
        private function onAdManagerEvent(_arg1:Event):void{
            Logger.info(this, ("onAdManagerEvent: " + _arg1.type));
            var _local2:XML = describeType(_arg1);
            var _local3:String = _local2.@name.toString();
            if (_local3.indexOf("com.tremormedia.acudeo.events::") == 0){
                _local3 = _local3.substr(31);
                switch (_local3){
                    case "AcudeoEvent":
                        dispatchEvent(new AcudeoEvent(_arg1));
                        break;
                    case "AdEvent":
                        dispatchEvent(new AdEvent(_arg1));
                        break;
                    case "AdErrorEvent":
                        dispatchEvent(new AcudeoEvent(_arg1));
                        break;
                    case "BannerEvent":
                        dispatchEvent(new BannerEvent(_arg1));
                        break;
                    case "BumperEvent":
                        dispatchEvent(new BumperEvent(_arg1));
                        break;
                    case "ControlsEvent":
                        dispatchEvent(new ControlsEvent(_arg1));
                        break;
                    case "ControllerEvent":
                        break;
                    default:
                        Logger.warn(this, ("UnknownEvent: " + _local3));
                        break;
                };
            } else {
                Logger.warn(this, ("UnknownEvent: " + _local3));
            };
        }
        private function onLoadComplete(_arg1:Event):void{
            removeListeners();
            _loadTimer.stop();
            _adManager = _arg1.target.content.getAdManager();
            _adManager.addEventListener("*", onAdManagerEvent);
            addChild((_adManager as Sprite));
            _adManager.init(_params);
            if (_loadParams != null){
                _adManager.load(_loadParams);
                _loadParams = null;
            };
            if (_videoRegion != null){
                _adManager.setVideoRegion(_videoRegion);
            };
            if (_volume != null){
                _adManager.setVolume((_volume as Number));
            };
            if (_content != null){
                _adManager.setContent(_content);
            };
            if (_playlist != null){
                _adManager.setPlaylist(_playlist);
            };
            if (_destroyCalled){
                _adManager.destory();
            };
            _adManagerLoaded = true;
        }
        public function init(_arg1:Object):void{
            var _local5:*;
            Logger.info(this, "init");
            _params = {host:DEFAULT_HOST, timeout:DEFAULT_TIMEOUT};
            if (_arg1 != null){
                for (_local5 in _arg1) {
                    if (_arg1[_local5] != null){
                        _params[_local5] = _arg1[_local5];
                        Logger.info(this, ((_local5 + "=") + _arg1[_local5]));
                    };
                };
            };
            var _local2:String = ((_params.host + "/embed/swf/acudeo.swf?rand=") + Math.floor((Math.random() * 1000000000)));
            if (_params.swfUrl){
                _local2 = _params.swfUrl;
            };
            Security.allowDomain(_local2);
            _loadTimer = new Timer(_params.timeout, 1);
            _loadTimer.addEventListener(TimerEvent.TIMER, onLoadTimeout);
            var _local3:ApplicationDomain = new ApplicationDomain();
            var _local4:LoaderContext = new LoaderContext(false, _local3);
            _local4.checkPolicyFile = true;
            _local4.securityDomain = SecurityDomain.currentDomain;
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
            _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
            _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
            _loader.load(new URLRequest(_local2), _local4);
            _loadTimer.start();
        }
        public function contentStop():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to contentStop before program loaded, dispatching adsComplete");
                dispatchEvent(new AcudeoEvent({type:AcudeoEvent.ADS_COMPLETE}));
            } else {
                _adManager.contentStop();
            };
        }
        public function contentResumed():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to contentResumed before program loaded, ignoring");
            } else {
                _adManager.contentResumed();
            };
        }
        public function contentPreStart():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to pauseAd before program loaded, starting content");
                dispatchEvent(new AcudeoEvent({type:AcudeoEvent.START_CONTENT}));
            } else {
                _adManager.contentPreStart();
            };
        }
        public function contentEnd():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to contentEnd before program loaded, dispatching adsComplete");
                dispatchEvent(new AcudeoEvent({type:AcudeoEvent.ADS_COMPLETE}));
            } else {
                _adManager.contentEnd();
            };
        }
        public function setVolume(_arg1:Number):void{
            if (!_adManagerLoaded){
                _volume = _arg1;
            } else {
                _adManager.setVolume(_arg1);
            };
        }
        private function dispatchError():void{
            dispatchEvent(new AcudeoEvent({type:AcudeoEvent.ERROR}));
        }
        public function resumeAd():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to resumeAd before program loaded, ignoring");
            } else {
                _adManager.resumeAd();
            };
        }
        public function setContent(_arg1:Object):void{
            if (!_adManagerLoaded){
                _content = _arg1;
            } else {
                _adManager.setContent(_arg1);
            };
        }
        public function loadAd(_arg1:Object):void{
            trace(("loadAd " + _arg1));
        }
        private function onLoadError(_arg1:IOErrorEvent):void{
            Logger.error(this, ("Error: " + _arg1.text));
            removeListeners();
            dispatchError();
        }
        private function onLoadTimeout(_arg1:Event):void{
            Logger.error(this, "AdManager load timeout");
            _loadTimer.stop();
            removeListeners();
            dispatchError();
        }
        private function removeListeners():void{
            if (((!((_loader == null))) && (!((_loader.contentLoaderInfo == null))))){
                _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
                _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
                _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoadProgress);
            };
        }
        public function pauseAd():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to pauseAd before program loaded, ignoring");
            } else {
                _adManager.pauseAd();
            };
        }
        public function contentProgress(_arg1:Number, _arg2:Number):void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to contentProgress before program loaded, ignoring");
            } else {
                _adManager.contentProgress(_arg1, _arg2);
            };
        }
        public function getCuePoints(_arg1:AdPosition):Array{
            var _local4:uint;
            if (!_adManagerLoaded){
                return (null);
            };
            var _local2:Array = _adManager.getCuePoints(_arg1);
            var _local3:Array = new Array();
            if (_local2 != null){
                _local4 = 0;
                while (_local4 < _local2.length) {
                    _local3.push(new CuePoint(_local2[_local4]));
                    _local4++;
                };
            };
            if (_local3.length == 0){
                return (null);
            };
            return (_local3);
        }
        public function getControlsState():ControlsState{
            return (new ControlsState(_adManager.getControlsState()));
        }
        public function setPlaylist(_arg1:Object):void{
            if (!_adManagerLoaded){
                _playlist = _arg1;
            } else {
                _adManager.setPlaylist(_arg1);
            };
        }
        public function load(_arg1:Object):void{
            Logger.info(this, "load");
            if (_adManagerLoaded){
                _adManager.load(_arg1);
            } else {
                _loadParams = _arg1;
            };
        }
        public function getAdXML():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to getAdXML before program loaded, ignoring");
            } else {
                _adManager.getAdXML();
            };
        }
        public function openSlate():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to openSlate before program loaded, ignoring");
            } else {
                _adManager.openSlate();
            };
        }
        private function onLoadProgress(_arg1:ProgressEvent):void{
            var _local2:Number = Math.round(((_arg1.bytesLoaded / _arg1.bytesTotal) * 100));
            if (_local2 > _prevProgress){
                trace((("AdManager " + _local2) + "% loaded"));
                _prevProgress = _local2;
            };
        }
        public function sendEvent(_arg1:String, _arg2:Object=null):void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to sendEvent before program loaded, ignoring");
            } else {
                _adManager.sendEvent(_arg1, _arg2);
            };
        }
        public function destroy():void{
            if (!_adManagerLoaded){
                _destroyCalled = true;
            } else {
                _adManager.destroy();
            };
        }
        public function endSlate():void{
            if (!_adManagerLoaded){
                Logger.warn(this, "Call to endSlate before program loaded, ignoring");
            } else {
                _adManager.endSlate();
            };
        }

        public static function getInstance():IAdManager{
            if (_instance == null){
                _instance = new (AdManager);
            };
            return (_instance);
        }

    }
}//package com.tremormedia.acudeo.admanager 
